package com.luke.star.serviceImpl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.luke.star.bean.StReportReq;
import com.luke.star.bean.StTaskNoteVo;
import com.luke.star.bean.enums.ReportDateTypeEnum;
import com.luke.star.persist.mapper.StTaskMapper;
import com.luke.star.persist.mapper.StTaskNoteMapper;
import com.luke.star.util.DateUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class StReportService {
	@Resource
	private StTaskMapper stTaskMapper;
	@Resource
	private StTaskNoteMapper stTaskNoteMapper;
	
	public String weixinDailyReport(StReportReq req) {
		
		return null;
	}
	
	public List<StTaskNoteVo> queryReportData(StReportReq req) throws Exception{
		log.info("请求报表数据-----req：{}",req.toString());
		String beginDateStr = "";
		String endDateStr = "";
		
		switch(req.getDateType()) {
			case "02":{//本月
				Calendar c = Calendar.getInstance();
				c.set(Calendar.DAY_OF_MONTH, 1);
				beginDateStr = DateUtil.getDateTimeToString(c.getTime(), "yyyyMMdd");
				endDateStr = DateUtil.getNowDateStr("yyyyMMdd");
				break;
			}
			case "03":{//日/周
				Date selectday = DateUtil.getStringToDateTime(req.getDateStr(), "yyyyMMdd");
				beginDateStr = DateUtil.getDateTimeToString(DateUtil.getFirstDayOfWeek(selectday), "yyyyMMdd");
				endDateStr = DateUtil.getDateTimeToString(DateUtil.getLastDayOfWeek(selectday), "yyyyMMdd");
				break;
			}
			case "04":;//日/月
			case "05":{//周/月
				Date selectday = DateUtil.getStringToDateTime(req.getDateStr(), "yyyyMMdd");
				Calendar c = Calendar.getInstance();
				c.setTime(selectday);
				c.set(Calendar.DAY_OF_MONTH, 1);
				beginDateStr = DateUtil.getDateTimeToString(c.getTime(), "yyyyMMdd");
				c.add(Calendar.MONTH, 1);
				c.add(Calendar.DAY_OF_YEAR, -1);
				endDateStr = DateUtil.getDateTimeToString(c.getTime(), "yyyyMMdd");
				break;
			}
			case "06":{//月/年
				Date selectday = DateUtil.getStringToDateTime(req.getDateStr(), "yyyy");
				Calendar c = Calendar.getInstance();
				c.setTime(selectday);
				c.set(Calendar.DAY_OF_YEAR, 1);
				beginDateStr = DateUtil.getDateTimeToString(c.getTime(), "yyyyMMdd");
				c.add(Calendar.YEAR, 1);
				c.add(Calendar.DAY_OF_YEAR, -1);
				endDateStr = DateUtil.getDateTimeToString(c.getTime(), "yyyyMMdd");
				break;
			}
			default:;
			case "01":{//本周
				beginDateStr = DateUtil.getDateTimeToString(DateUtil.getFirstDayOfWeek(new Date()), "yyyyMMdd");
				endDateStr = DateUtil.getNowDateStr("yyyyMMdd");
				break;
			}
		}
		
		List<StTaskNoteVo> starlist = new ArrayList<>();
		if(ReportDateTypeEnum.monthyear.code.equals(req.getDateType())) {
			starlist = stTaskNoteMapper.listTaskLogByMonth(beginDateStr,endDateStr);
		}else {
			starlist = stTaskNoteMapper.listTaskLogByDay(beginDateStr,endDateStr);
		}
		
		if(ReportDateTypeEnum.weekmonth.code.equals(req.getDateType())) {
			starlist = sumWeekOfMonthData(starlist);
		}
		
		//分数计算
		for(StTaskNoteVo bean:starlist) {
			int score = bean.getWeight()*bean.getSumStar();
			bean.setScore(score);
		}
		
		//TODO 任务新增维护
		
		return starlist;
		//TODO 中间值一定要显示，初始数据为0嘛
		
	}
	
	
	private List<StTaskNoteVo> sumWeekOfMonthData(List<StTaskNoteVo> starlist){
		List<StTaskNoteVo> newlist = new ArrayList<>(); 
		Map<String,List<StTaskNoteVo>> map = new LinkedHashMap<>();
		for(StTaskNoteVo bean:starlist) {
			if(map.containsKey(bean.getTaskId())) {
				List<StTaskNoteVo> list = map.get(bean.getTaskId());
				list.add(bean);
			}else {
				List<StTaskNoteVo> list = new ArrayList<>();
				list.add(bean);
				map.put(bean.getTaskId(),list);
			}
		}
		for(List<StTaskNoteVo> list:map.values()) {
			StTaskNoteVo bean0 = starlist.get(0);
			StTaskNoteVo sumbean = new StTaskNoteVo();
			BeanUtils.copyProperties(bean0, sumbean);
			sumbean.setSumStar(0);
			Date day = DateUtil.getStringToDateTimeExceptionNull(bean0.getDayStr(), DateUtil.DATE_FORMAT_YYYYMMDD);
			Date weekEnd = DateUtil.getLastDayOfWeek(day);
			for(StTaskNoteVo bean:list) {
				Date beanday = DateUtil.getStringToDateTimeExceptionNull(bean0.getDayStr(), DateUtil.DATE_FORMAT_YYYYMMDD);
				if(beanday.compareTo(weekEnd) > 0 ) {
					newlist.add(sumbean);
					weekEnd = DateUtil.getLastDayOfWeek(beanday);
					sumbean = new StTaskNoteVo();
					BeanUtils.copyProperties(bean,sumbean);
				}else {
					sumbean.setSumStar(sumbean.getSumStar()+bean.getSumStar());
				}
				
			}
			newlist.add(sumbean);
		}
		return newlist;
	}
	
	
}
